package log4Compile;

import java.io.*;

import java.util.Date;
import java.text.SimpleDateFormat;

/*
    日志类
    2022-3-9-杨海南
 */

public class Log {
//    File logFile = new File("src/main/java/log4Compile/log.txt");
    File logFile = new File("log.txt");

    private Log() {
    }

    /*
        0:开发模式(输出所有)                  --输出方法进入返回
        1:INFO模式(输出绝大部分详细信息)        --输出输入与树与token流
        2:DEBUG模式（输出大部分信息）          --输出总体步骤
        3:默认                              --
        4:输出错误流                         --错误信息，错误流输出
        5:仅记录报错                         --错误信息
     */

    private int logLevel = 3;
    private static Log logInstance = new Log();

    public static Log getLogInstance() {
        return logInstance;
    }

    public void setLogLevel(int logLevel) {
        logger("已将日志级别设定为"+logLevel,5);
        this.logLevel = logLevel;
    }

    public void logger(String msg, int level) {
        if (level >= logLevel) { //错误信息 错误信息流
            try {
                FileWriter logWrite = new FileWriter(logFile, true);
                PrintWriter logPrint = new PrintWriter(logWrite);
                //设置日期格式
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                logPrint.println(df.format(new Date()) + "--" + msg);
                logPrint.close();
                logWrite.close();
            } catch (FileNotFoundException ex) {
                System.out.println("File Not Found !");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public int getLogLevel() {
        return this.logLevel;
    }
}
